package cn.cboard.gateway;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * @author
 *
 *  Class that represents a part of a where clause.
 * 
 */
public class WhereDesc implements Serializable {

	protected String phrase;
	protected ColumnDesc[] params;
	protected int numberOfParams;

	public WhereDesc(String phrase) {
		this.phrase = phrase;
		params = null;
		resetParamCount();
	}

	protected void resetParamCount() {
		if (phrase == null) {
			numberOfParams = 0;
		}
		else {
			char[] arr = phrase.toCharArray();
			int count = 0;
			for (int i = 0; i < arr.length; ++i) {
				if (arr[i] == '?')
					++count;
			} //rof
			numberOfParams = count;

		} //fi
	}

	

	/**
	 * Returns the numberOfParams.
	 * @return int
	 */
	public int getNumberOfParams() {
		return numberOfParams;
	}

	
    
     /**
     * Returns the params.
     * @return Object[]
     */
    public Collection getNamedParams(Map map) {
        if (params == null) {
            return   new ArrayList();
        }
        ArrayList paramValues = new ArrayList();
        // check all columns are in the map
        for (int i = 0; i < params.length; ++i) {
            ColumnDesc d = params[i];
            if (map.containsKey(d.getColumn())) {
                Object value = map.get(d.getColumn());
                Object convertedValue = d.getParam(value);
                paramValues.add(convertedValue);
            } else {
               return new ArrayList();
            }
            
        } // rof
        return paramValues;
    }

	/**
	 * Returns the phrase.
	 * @return String
	 */
	public String getPhrase() {
		return phrase;
	}

	
	/**
	 * Sets the phrase.
	 * @param phrase The phrase to set
	 */
	public void setPhrase(String phrase) {
		this.phrase = phrase;
		resetParamCount();
	}

	public String getWhereClause(Object[] params, int index) {
		if (numberOfParams == 0) {
			return getPhrase();
		}
        
          
		//check for null params
		//int total = index + numberOfParams;
		for (int i = index; i < params.length; ++i) {
			if (params[i] == null)
				return null;
		} //rof
		return getPhrase();
	}
    
  
    public String getWhereClause(Map map) {
        if (params == null) {
            return getPhrase();
        }
        // check all columns are in the map
        for (int i = 0; i < params.length; ++i) {
            String colName = params[i].getColumn();
            if (!map.containsKey(colName)) {
                return null;
            } //fi
            
        } // rof
        return getPhrase();
    }
    

	/**
	 * Returns the params.
	 * @return ColumnDesc[]
	 */
	public ColumnDesc[] getParams() {
		return params;
	}

	/**
	 * Sets the params.
	 * @param params The params to set
	 */
	public void setParams(ColumnDesc[] params) {
		this.params = params;
	}

}
